<?php
class Report_controller extends MY_Controller
{
	var $current_row=0;
	var $data_absen=null;
	var $tgl_mulai;
	var $tgl_sampai;	
	var $nama_perusahaan;
	
	function __Construct($db)
	{		
		parent::__construct();			
		if(!isset($this->db))
		{
			$this->db=$db;
		}
		
		$this->load->model("departemen");
		$this->load->model("record_absen");
		$this->load->model("rekap_absen");
		$this->load->helper("form");
		
		$this->excel = $this->create_excel();
		$this->sheet = $this->excel->setActiveSheetIndex(0);		
	}

	function set_page_number()
	{
		$this->sheet->getHeaderFooter()->setOddFooter('&L&B' . $this->nama_perusahaan . '&RPage &P of &N');	
	}
	
	function add_row($row=1)
	{
		$this->current_row+= $row;
	}
	
	function save_as_excel5($tgl_mulai,$tgl_sampai,$title="absensi")
	{
		$this->__write_excel_header($title,$tgl_mulai);
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
		$objWriter->save('php://output'); 			
	}
	
	function save_as_pdf($title="absen")
	{
		//save file
		$filexls = "c:\\".$title."-".date("ymd-His").".xls";
		$filepdf = "c:\\".$title."-".date("ymd-His").".pdf";
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
		$objWriter->save($filexls);	
		
		//convert
		$jodconvert = $this->config->item("jodconvert");
		shell_exec("$jodconvert $filexls");
		
		
		if(file_exists($filepdf))
		{
		
			$f = fopen($filepdf,"r");
			$contents = stream_get_contents($f);
			$filesize=($filepdf);
			fclose($f);
		
			if(file_exists($filepdf)){
				unlink($filepdf);
			}

			if(file_exists($filexls)){
				unlink($filexls);
			}
			
			header("Content-type: application/pdf");
			echo $contents;	
		}
		else
		{
			redirect("laporan/main/gagal");
		}
	}

	function add_border($area,$sheet)
	{
		$styleArray = array(
		  'borders' => array(
			'allborders' => array(
			  'style' => PHPExcel_Style_Border::BORDER_THIN
			)
		  )
		);	
		
		$sheet->getStyle($area)->applyFromArray($styleArray);
	}
	
	function set_print_area()
	{
		//$this->sheet->getStyle('A1:N100')->getAlignment()->setWrapText(true);
		$sheet = $this->excel->getActiveSheet();	
		$objPageSetup = $sheet->getPageSetup();
		$objPageSetup->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER);
		$objPageSetup->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);

		//$objPageSetup->setPrintAreaByColumnAndRow(0, 1, 13, $this->current_row[$sheet]);
		//$objPageSetup->setFitToPage(true);
		//$objPageSetup->setFitToWidth(1);
		//$objPageSetup->setFitToHeight(1);
		$objPageSetup->setHorizontalCentered(true);
		$objPageSetup->setScale(75,false);

		$margin = $sheet->getPageMargins();
		$margin->setLeft((0.60/2.54))
		->setRight((0.60/2.54))
		->setTop((1.90/2.54))
		->setBottom((1.90/2.54));
	  
		//set repeat
		$objPageSetup->setRowsToRepeatAtTopByStartAndEnd(1, 3);
		$sheet->setPageSetup($objPageSetup);

	}
	
	
	function set_bolds($cell_array)
	{
		if(is_array($cell_array))
		{
			foreach($cell_array as $cell)
			{
				$this->sheet->getStyle($cell)->getFont()->setBold(true);
			}
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function set_horizontals($cell_array)
	{
		if(is_array($cell_array))
		{
			foreach($cell_array as $cell)
			{
				$this->set_horizontal($cell);
			}
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function set_horizontal($cell,$align=PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
	{
		$this->sheet->getStyle($cell)->getAlignment()
		->applyFromArray(
		array(
							  		'horizontal'=>$align,
							  	    'wrap'=>true
		)
		);
	}

	function set_borders($cell_array)
	{
		if(is_array($cell_array))
		{
			foreach($cell_array as $cell)
			{
				$this->set_border($cell);
			}
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function set_border($cell)
	{
		$this->sheet->getStyle($cell)->getBorders()
		->getAllBorders()
		->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)
		->getColor()->applyFromArray(array('rgb'=>"000000"))
		;

	}
	
	function break_row($row=false)
	{
		//break row
		if($row==false)
		{
			$this->sheet->setBreak( 'A'.$this->current_row, PHPExcel_Worksheet::BREAK_ROW );
		}
		else
		{
			$this->sheet->setBreak( 'A'.$row , PHPExcel_Worksheet::BREAK_ROW );
		}
	}
	
}
?>